package com.lancoo.znbkxx.teacher.diagnosis

import android.graphics.Color
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry
import java.math.BigDecimal

/**
 * @author : Admin
 * @date   : 2020/11/30
 * @desc   :
 */
class PieChartManager(private val pieChart: PieChart, val type: String, val percent: Int) {
    init {
        initPieChart()
    }

    private fun initPieChart() {
        pieChart.setExtraOffsets(0f, 15f, 0f, 15f)
        //  是否显示中间的洞
        pieChart.isDrawHoleEnabled = false
        pieChart.legend.isEnabled = false
        pieChart.isRotationEnabled = false // 可以手动旋转
        pieChart.description.isEnabled = false //取消右下角描述
        //是否显示每个部分的文字描述
        pieChart.setDrawEntryLabels(true)
        pieChart.setEntryLabelTextSize(10f)
        pieChart.setEntryLabelColor(Color.parseColor("#788aa4"))
        pieChart.animateXY(1000, 1000)
    }

    /**
     * 显示实心圆
     * @param yvals
     * @param colors
     */
    fun showSolidPieChart(yvals: MutableList<PieEntry>, colors: MutableList<Int>) {
        pieChart.isDrawHoleEnabled = false
        //数据集合
        val dataSet = PieDataSet(yvals, type)
        //填充每个区域的颜色
        dataSet.colors = colors
        //是否在图上显示数值
        dataSet.setDrawValues(true)
        dataSet.valueTextSize = 10f
        dataSet.setValueTextColors(colors)
        dataSet.valueLinePart1Length = 0.9f
        dataSet.valueLinePart2Length = 2.7f
        dataSet.valueLinePart1OffsetPercentage = 105f
        dataSet.xValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
        dataSet.yValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
        //        设置每条之前的间隙
        dataSet.sliceSpace = 1f
        //设置饼状Item被选中时变化的距离
        dataSet.selectionShift = 2f
        dataSet.setUseValueColorForLine(true)
        setSolidDataFormat(dataSet)
        //填充数据
        val pieData = PieData(dataSet)
        pieChart.data = pieData
    }


    /**
     * 显示圆环
     * @param yvals
     * @param colors
     */
    fun showRingPieChart(yvals: MutableList<PieEntry>, colors: MutableList<Int>) {
        //显示为圆环
        pieChart.isDrawHoleEnabled = true
        pieChart.holeRadius = 77f //设置中间洞的大小
        //数据集合
        val dataSet = PieDataSet(yvals, type)
        //填充每个区域的颜色
        dataSet.colors = colors
        //是否在图上显示数值
        dataSet.setDrawValues(true)
        dataSet.valueTextSize = 10f
        dataSet.setValueTextColors(colors)
        dataSet.valueLinePart1Length = 0.3f
        dataSet.valueLinePart2Length = 1f
        dataSet.valueLinePart1OffsetPercentage = 105f
        dataSet.xValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
        dataSet.yValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
        //        设置每条之前的间隙
        dataSet.sliceSpace = 1f
        //设置饼状Item被选中时变化的距离
        dataSet.selectionShift = 2f
        dataSet.setUseValueColorForLine(true)
        setRingDataFormat(dataSet, type)
        //填充数据
        val pieData = PieData(dataSet)
        pieChart.data = pieData
    }

    private fun setRingDataFormat(dataSet: PieDataSet, type: String) {
        dataSet.setValueFormatter { value, entry, dataSetIndex, viewPortHandler ->
            val label = (entry as PieEntry).label
            if (type == "words") {
                return@setValueFormatter when {
                    label.contains("尚未学习") -> {
                        "尚未学习词汇"
                    }
                    label.contains("总词汇量") -> {
                        "总词汇量"
                    }
                    else -> {
                        label
                    }
                }
            } else {
                return@setValueFormatter if (label.contains("语法")) {
                    "总语法量"
                } else {
                    "已掌握"
                }
            }
        }
    }

    private fun setSolidDataFormat(dataSet: PieDataSet) {
        dataSet.setValueFormatter { value, entry, dataSetIndex, viewPortHandler ->
            val label = (entry as PieEntry).label
            if (label.contains("尚未学习")) {
                return@setValueFormatter "个,占比${percent}%"
            } else {
                return@setValueFormatter "个,占比${
                    BigDecimal(value * 100.toDouble() / pieChart.data.yValueSum).setScale(0,
                        BigDecimal.ROUND_HALF_UP)
                }%"
            }
        }
    }
}
